package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbUserMapper;
import com.bwie.domain.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.RoleEnum;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author : xiaogao
 * @date : 2024-11-14 13:58
 * @description :
 **/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {
    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public TbUser getUserById(Integer userId) {
        return tbUserMapper.selectById(userId);
    }

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        // 获取用户信息
        TbUser tbUser = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName,loginInfoVo.getUserName())
        );
        if(tbUser==null){
            return R.ERROR(400,"用户不存在");
        }
        if(!tbUser.getUserPass().equals(loginInfoVo.getUserPass())){
            return R.ERROR(400,"密码不存在");
        }

        // 生成toKen
        String token = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,"bwie")
                .claim("userId",tbUser.getUserId())
                .claim("userName",tbUser.getUserName())
                .claim("nickName",tbUser.getNickName())
                .compact();
        // 存缓存
        String key = "token" + tbUser.getUserId();
        stringRedisTemplate.opsForValue().set(key,token,30, TimeUnit.MINUTES);

        tbUser.setToken(token);
        return R.OK(tbUser);
    }

    // 商家注册接口
    @Override
    public R shopperRegister(TbUser tbUser) {
        // 验证用户名是否存在
        TbUser user = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName,tbUser.getUserName())
        );
        if(user !=null){
            return R.ERROR(400,"用户已存在");
        }
        TbUser user2 = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserMobile,tbUser.getUserMobile())
        );
        if(user2 != null){
            return R.ERROR(400,"手机号已存在");
        }
        tbUser.setUserRole(RoleEnum.ROLE_SHOPPER.getCode());
        tbUser.setCreateTime(new Date());
        tbUserMapper.insert(tbUser);
        return R.OK();
    }

    @Override
    public R sendKafkaMessage(String message) {
        // 第一个参数 ：topic
        // 第二个参数 ：发送的消息
        kafkaTemplate.send("topic-test",message);
        return R.OK();
    }

    @KafkaListener(topics = "topic-test")
    public void receiveTestTopicMessage(String message, Acknowledgment acknowledgment){
        //接收消息
        System.out.println("接收到消息："+message);
        // 手动提交消费并删除
        acknowledgment.acknowledge();
    }
}